package cn.wildfire.chat.kit;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.text.TextUtils;

import androidx.core.app.NotificationCompat;

import com.sourcemuch.im.caiba.CaiBaChatApp;
import com.sourcemuch.im.caiba.R;import com.sourcemuch.im.caiba.R2;
import com.sourcemuch.im.caiba.SuperService;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import cn.wildfire.chat.app.main.MainActivity;
import cn.wildfire.chat.kit.conversation.ConversationActivity;
import cn.wildfirechat.message.Message;
import cn.wildfirechat.message.MessageContentMediaType;
import cn.wildfirechat.message.StickerMessageContent;
import cn.wildfirechat.message.TextMessageContent;
import cn.wildfirechat.message.core.MessageContentType;
import cn.wildfirechat.message.core.MessageDirection;
import cn.wildfirechat.message.notification.RecallMessageContent;
import cn.wildfirechat.model.Conversation;
import cn.wildfirechat.model.ConversationInfo;
import cn.wildfirechat.model.GroupInfo;
import cn.wildfirechat.remote.ChatManager;

import static androidx.core.app.NotificationCompat.CATEGORY_MESSAGE;
import static androidx.core.app.NotificationCompat.DEFAULT_ALL;
import static cn.wildfirechat.message.core.PersistFlag.Persist_And_Count;
import static cn.wildfirechat.model.Conversation.ConversationType.Single;

public class WfcNotificationManager {
    private WfcNotificationManager() {

    }

    private static WfcNotificationManager notificationManager;

    public synchronized static WfcNotificationManager getInstance() {
        if (notificationManager == null) {
            notificationManager = new WfcNotificationManager();
        }
        return notificationManager;
    }

    public void clearAllNotification(Context context) {
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.cancelAll();
        notificationConversations.clear();
    }

    /**
     * modify by HEKE , 2019/8/24 18:26
     * TODO 野火IM接收所有信息通道
     *
     * @param context
     * @param tag
     * @param id
     * @param title
     * @param content
     * @param pendingIntent
     * @param message
     */
    private void showNotification(Context context, String tag, int id, String title, String content, PendingIntent pendingIntent, Message message) {
        /**
         * modify by HEKE , 2019/8/24 18:25
         * TODO 当前通信若是屏蔽消息状态则不进行通知
         */
        if (!isCuurentReceiverMessageSilent) {
            NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            String channelId = SuperService.CHANNEL_ID;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                notificationManager.deleteNotificationChannel(channelId);
                NotificationChannel channel = new NotificationChannel(channelId,
                        SuperService.CHANNEL_NAME,
                        NotificationManager.IMPORTANCE_HIGH);
                channel.enableLights(true); //是否在桌面icon右上角展示小红点
                channel.setLightColor(Color.GREEN); //小红点颜色
                channel.setShowBadge(true); //是否在久按桌面图标时显示此渠道的通知
                if (isCuurentReceiverMessageSilent) {
                    channel.setSound(null, null);
                }
                notificationManager.createNotificationChannel(channel);
            }

            NotificationCompat.Builder builder = new NotificationCompat.Builder(context, channelId)
                    .setSmallIcon(R.mipmap.ic_launcher_cailiao)
                    .setAutoCancel(true)
                    .setCategory(CATEGORY_MESSAGE)
                    .setDefaults(DEFAULT_ALL);
            builder.setContentIntent(pendingIntent);
            builder.setContentTitle(context.getString(R.string.app_name));
            /**
             * 处理不同类型数据的锁屏通知显示内容
             * */
            boolean nullContent = (content == null || content.length() == 0 ||
                    content.toLowerCase().equals("null") || content.toLowerCase().contains("]null"));
            String showContent = nullContent ? "" : content;
            if (nullContent) {
                if (message.content instanceof StickerMessageContent) {
                    StickerMessageContent stickerMessageContent = (StickerMessageContent) message.content;
                    if (stickerMessageContent.mediaType == MessageContentMediaType.VOICE) {
                        showContent = "[" + context.getString(R.string.voice_noi) + "]";
                    } else if (stickerMessageContent.mediaType == MessageContentMediaType.FILE) {
                        showContent = "[" + context.getString(R.string.file_noi) + "]";
                    } else if (stickerMessageContent.mediaType == MessageContentMediaType.VIDEO) {
                        showContent = "[" + context.getString(R.string.video_noi) + "]";
                    } else if (stickerMessageContent.mediaType == MessageContentMediaType.IMAGE) {
                        showContent = "[" + context.getString(R.string.picture_noi) + "]";
                    }
                }
            }
            builder.setContentText(title + ":" + showContent);
            Notification notification = null;
            if (isCuurentReceiverMessageSilent) {
                builder.setSound(null);
                notification = builder.build();
            } else {
                notification = builder.build();
                notification.defaults = Notification.DEFAULT_SOUND;
            }
            notificationManager.notify(tag, id, notification);
            ((CaiBaChatApp) context.getApplicationContext()).receiveMessage(content, title);
        }
    }

    /**
     * 当前接收到的消息是否静音
     */
    protected boolean isCuurentReceiverMessageSilent;

    public void handleRecallMessage(Context context, Message message) {
        handleReceiveMessage(context, Collections.singletonList(message));
    }

    public void handleReceiveMessage(Context context, List<Message> messages) {

        if (messages == null || messages.isEmpty()) {
            return;
        }

        try {
            ConversationInfo conversationInfo = ChatManager.Instance().getConversation(messages.get(0).conversation);
            if (conversationInfo != null) {
                isCuurentReceiverMessageSilent = conversationInfo.isSilent;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        for (Message message : messages) {
            if (message.direction == MessageDirection.Send || (message.content.getPersistFlag() != Persist_And_Count && !(message.content instanceof RecallMessageContent))) {
                continue;
            }
            String pushContent = message.content.encode().pushContent;
            if (TextUtils.isEmpty(pushContent)) {
                if (message.content.getType() == MessageContentType.ContentType_Text) {
                    TextMessageContent textMessageContent = (TextMessageContent) message.content;
                    pushContent = textMessageContent.getContent();
                } else if (message.content.getType() == MessageContentType.ContentType_Image) {
                    pushContent = "[图片]";
                } else if (message.content.getType() == MessageContentType.ContentType_Voice) {
                    pushContent = "[语音]";
                } else if (message.content.getType() == MessageContentType.ContentType_Recall) {
                    pushContent = "撤回了一条消息";
                }
            }

            int unreadCount = ChatManager.Instance().getUnreadCount(message.conversation).unread;
            if (unreadCount > 1) {
                pushContent = "[" + unreadCount + "条]" + pushContent;
            }

            String title = "";
            if (message.conversation.type == Single) {
                String name = ChatManager.Instance().getUserDisplayName(message.conversation.target);
                title = TextUtils.isEmpty(name) ? "新消息" : name;
            } else if (message.conversation.type == Conversation.ConversationType.Group) {
                GroupInfo groupInfo = ChatManager.Instance().getGroupInfo(message.conversation.target, false);
                title = groupInfo == null ? "群聊" : groupInfo.name;
            } else {
                title = "新消息";
            }
            Intent mainIntent = new Intent(context, MainActivity.class);
            Intent conversationIntent = new Intent(context, ConversationActivity.class);
            conversationIntent.putExtra("conversation", message.conversation);
            PendingIntent pendingIntent = PendingIntent.getActivities(context, notificationId(message.conversation), new Intent[]{mainIntent, conversationIntent}, PendingIntent.FLAG_UPDATE_CURRENT);
            String tag = "wfc notification tag";
            showNotification(context, tag, notificationId(message.conversation), title, pushContent, pendingIntent, message);
        }
    }

    private List<Conversation> notificationConversations = new ArrayList<>();

    private int notificationId(Conversation conversation) {
        if (!notificationConversations.contains(conversation)) {
            notificationConversations.add(conversation);
        }
        return notificationConversations.indexOf(conversation);
    }
}
